import os
import pygame
from pygame.locals import *

import locals

class GameObject(pygame.sprite.DirtySprite):
    def __init__(self, img, *groups):
        pygame.sprite.DirtySprite.__init__(self, *groups)
        self.image = pygame.image.load(os.path.join('data', 'images', img))
        self.rect = self.image.get_rect()
        self.dirty = 0
        self.visible = 0
        self.blendmode = 0
        self.jumpspeed = 0
        self.fallspeed = 0
        
        self.collision_groups = []
    
    def setup(self, **kwargs):
        """for k in kwargs:
            print k, kwargs[k]"""
        
        for k in kwargs:
            self.setAttr(k, kwargs[k])
    
    def setAttr(self, attr, val):
        exec("self.%s = %s" % (attr, val))
    
    def addCollisionGroup(self, group):
        self.collision_groups.append(group)
    
    def setCenterX(self, x):
        self.rect.centerx = x

    def setCenterY(self, y):
        self.rect.centery = y
    
    def move(self, dX, dY, collide = True):
        if collide:
            if dX:
                dX, dummy = self._step(dX, 0)
            if dY:
                dummy, dY = self._step(0, dY)
        else:
            self.rect.move_ip(dX, dY)
        return dX, dY
    
    def _step(self, dX, dY):
        oldrect = self.rect
        self.rect.move_ip(dX*3, dY*3)
        
        if abs(dX) > abs(dY): dY = 0
        else: dx = 0
        if dY < 0: side = 0
        elif dX > 0: side = 1
        elif dY > 0: side = 2
        elif dX < 0: side = 3
        
        for group in self.collision_groups:
            for spr in group:
                if spr.rect.colliderect(self.rect):
                    if side is 0:
                        self.rect.top = spr.rect.bottom
                        self.jumping = False
                        self.jumpspeed = 0
                    if side is 1:
                        self.rect.right = spr.rect.left
                    if side is 2:
                        self.falling = False
                        self.fallingspeed = 0
                        self.rect.bottom = spr.rect.top
                    if side is 3:
                        self.rect.left = spr.rect.right
        return self.rect.left - oldrect.left, self.rect.top - oldrect.top
    
    def jump(self):
        if not self.jumping and not self.falling:
            self.jumping = True
            self.falling = False
            self.jumpspeed = -locals.PLAYER['JUMP_HEIGHT']
    
    def update(self):
        
        if self.jumping:
            if self.jumpspeed > 0:
                self.jumping = False
                self.falling = True
                self.jumpspeed = 0
                self.fallspeed = -locals.PLAYER['JUMP_HEIGHT']
            elif self.jumpspeed < 0:
                self.jumping = True
                self.jumpspeed += locals.GRAVITY
        elif self.falling:
            pass
        
        keys = pygame.key.get_pressed()
        move = 0
        
        if keys[K_LEFT]:
            if self.jumping or self.falling:
                move = -locals.PLAYER['AIR_SPEED']
            else:
                move = -locals.PLAYER['GROUND_SPEED']
        elif keys[K_RIGHT]:
            if self.jumping or self.falling:
                move = locals.PLAYER['AIR_SPEED']
            else:
                move = locals.PLAYER['GROUND_SPEED']
            
        if keys[K_SPACE]:
            self.jump()
        
        self.move(move, self.jumpspeed or -self.fallspeed)